package com.badibadi.fragment;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.badibadi.adapter.XListView1Adapter_1;
import com.badibadi.infos.ClubListModel;
import com.badibadi.infos.Results;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.view.my_view.XListView;
import com.view.my_view.XListView.IXListViewListener;

public class IndexClub0_1Fragment extends BaseFragment implements
		IXListViewListener {
	private XListView1Adapter_1 x1Adapter;
	private XListView xListView1;
	private int Page;
	private String temp_content = null;
	private Message message;
	private Bundle bundle;
	// private Handler mHandler;
	private List<ClubListModel> clubListModels;
	private List<ClubListModel> list;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			try {

				switch (msg.what) {
				case 1:
					Utils.ExitPrgress(getActivity());
					try {
						Utils.showMessage(getActivity(), getResources()
								.getString(R.string.l_net_error));
						xListView1.stopRefresh();
						xListView1.stopLoadMore();
						xListView1.setRefreshTime(""+getResources().getString(R.string.ganggang));
					} catch (Exception e) {
						// TODO: handle exception
					}
				
					break;
				case 2:
					Utils.ExitPrgress(getActivity());
					try {
						clubListModels.addAll(list);
						x1Adapter.notifyDataSetChanged();
						xListView1.stopRefresh();
						xListView1.stopLoadMore();
						xListView1.setRefreshTime(""+getResources().getString(R.string.ganggang));
					} catch (Exception e) {
						// TODO: handle exception
					}
				
					break;
				case 3:
					try {
						Utils.ExitPrgress(getActivity());
						Utils.showMessage(getActivity(), getResources()
								.getString(R.string.l_xa10));
						xListView1.stopRefresh();
						xListView1.stopLoadMore();
						xListView1.setRefreshTime(""+getResources().getString(R.string.ganggang));
					} catch (Exception e) {
						// TODO: handle exception
					}
				
					break;
				default:
					break;
				}
			} catch (Exception e) {
				   
			}
		}

	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// x1Adapter = new XListView1Adapter();
		// mHandler = new Handler();
		clubListModels = new ArrayList<ClubListModel>();
		x1Adapter = new XListView1Adapter_1(clubListModels, getActivity());
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.after_landing_friends00, null);
		Page = 0;
		temp_content = getArguments().getString("editcontent");
		InitMyXListView1(view);
		// ClubSearch(Page, temp_content, null);
		return view;
	}

	public void InitMyXListView1(View view) {
		xListView1 = (XListView) view
				.findViewById(R.id.after_landing_friends00_xlistView);
		xListView1.setXListViewListener(this);
		xListView1.setPullLoadEnable(true);
		xListView1.setPullRefreshEnable(true);
		xListView1.setAdapter(x1Adapter);
		xListView1.startLoadMore();
	}

	private void ClubSearch(final int page, final String cont,
			final String typeid) {
		// Utils.showPrgress(getActivity());

		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				list = new ArrayList<ClubListModel>();
				message = new Message();
				bundle = new Bundle();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("type", "club");
				map.put("page", page);
				map.put("pageNum", 10);
				map.put("content", cont);
				if (typeid != null) {
					map.put("typeId", typeid);
				}
				map.put("uid", Utils.getUid(getActivity()));
//				   (map.toString());
				String content = Utils.sendRequest(map, Constants.BadiUrl
						+ "search");
				if (content != null) {
					Results results = Utils.checkResult_NNN(getActivity(),
							content);
//					   ("结果是:" + content);
					if (results != null && !results.getRetmsg().equals("null")) {
						try {
							list = JSONUtils.getListByJsonString(
									results.getRetmsg(), ClubListModel.class);
							// clubListModels.addAll(list);
							handler.sendEmptyMessage(2);
						} catch (JSONException e) {
							   
						}
					} else {
						if (page > 1) {
							--Page;
						} else {
							Page = 1;
						}
						handler.sendEmptyMessage(3);
					}
				} else {
					if (page > 1) {
						--Page;
					} else {
						Page = 1;
					}
					handler.sendEmptyMessage(1);
				}

			}
		});
	}

	// @Override
	// public void onListViewRefresh() {
	// // TODO Auto-generated method stub
	// ClubSearch(Page, temp_content, null);
	// }
	//
	// @Override
	// public void onListViewLoadMore() {
	// // TODO Auto-generated method stub
	// Utils.showPrgress(getActivity());
	// ClubSearch(++Page, temp_content, null);
	// }

	@Override
	public void onRefresh() {
		Page = 1;
		if (clubListModels != null) {
			clubListModels.clear();
		}
		ClubSearch(Page, temp_content, null);
	}

	@Override
	public void onLoadMore() {
		ClubSearch(++Page, temp_content, null);
	}
}